$(function() {
	var service = new EbeimService();
	var json = {};
	var user = plugin.cookieToJson(document.cookie);
	var jusername = $("#j-username"), jrole = $("#j-role");

	jusername.html(user.username);
	if (user.role == "SYSADMIN") {
		jrole.html("系统管理员");
	} else if (user.role == "ADMIN") {
		jrole.html("管理员");
	} else {
		jrole.html("普通用户");
	}
	service.getSystemInfo(json, callback1);
	$(".lab-check").bind("click", function() {
		var t = $(this);
		if (t.hasClass("check-true")) {
			t.removeClass("check-true");
			t.siblings("input[type='checkbox']").prop("checked", false);
		} else {
			t.addClass("check-true");
			t.siblings("input[type='checkbox']").prop("checked", true);
		}
	})
	$("#netcut").bind("click", function() {
		var t = $(this);
		if (t.hasClass("check-true")) {
			$("input[name='writeLimit']").attr("disabled", "disabled");
			$("input[name='readLimit']").attr("disabled", "disabled");
		} else {
			$("input[name='writeLimit']").removeAttr("disabled");
			$("input[name='readLimit']").removeAttr("disabled");
		}
	})
	$("#lock").bind("click", function() {
		var t = $(this);
		if (t.hasClass("check-true")) {
			$("#lockTimeSelect").removeAttr("disabled");
			// $("input[name='lockTime']").removeAttr("disabled");
		} else {
			$("#lockTimeSelect").attr("disabled", "disabled");
			// $("input[name='lockTime']").attr("disabled", "disabled");
		}
	})

})

var callback1 = function(data) {
	$("input[name='writeLimit']").val(data.writeLimit);
	$("input[name='readLimit']").val(data.readLimit);
	$("input[name='serverIp']").val(data.apiHost + ":" + data.port);
	setLockTime(data.lockTime);
	netcutSel('netcutSel', data.netcutSel);
	// setSel('rebuildsysSel', data.rebuildsysSel);
	// setSel('rebuildserSel', data.rebuildserSel);
	// setSel('saveboxSel', data.saveboxSel);
	setSel('softwaretipSel', data.softwaretipSel);
	setSel('tasktipSel', data.tasktipSel);
	setSel('noticetipSel', data.noticetipSel);
}

var callback = function(data) {
	if (data.code == "200" || data.code == 200) {
		$("input[name='writeLimit']").val(data.writeLimit);
		$("input[name='readLimit']").val(data.readLimit);
		$("input[name='serverIp']").val(data.apiHost + ":" + data.port);
		setLockTime(data.lockTime);
		netcutSel('netcutSel', data.netcutSel);
		// setSel('rebuildsysSel', data.rebuildsysSel);
		// setSel('rebuildserSel', data.rebuildserSel);
		// setSel('saveboxSel', data.saveboxSel);
		setSel('softwaretipSel', data.softwaretipSel);
		setSel('tasktipSel', data.tasktipSel);
		setSel('noticetipSel', data.noticetipSel);

		plugin.msgtip({
			type : "o",
			msg : "设置成功！"
		});
	} else {
		plugin.msgtip({
			type : "e",
			msg : "设置失败！"
		});
	}

}
function validate(str) {
	var reg = new RegExp("^[0-9]*$");
	var obj = document.getElementById("name");
	if (!reg.test(str)) {
		return false;
	}
	if (parseInt(str) > 12500 || parseInt(str) < 1) {
		return false;
	}
	return true;
}

function getSelStatus(str) {
	var obj = document.getElementById(str);
	if (obj.checked) {
		return 1;
	} else {
		return 0;
	}
}

function saveSys() {
	var writeLimit = $("input[name='writeLimit']").val().trim();
	var readLimit = $("input[name='readLimit']").val().trim();
	var lockSel = document.getElementById('lockSel');
	var netCut = getSelStatus('netcutSel');
	if (netCut == 0) {
		if (writeLimit.length == 0) {
			plugin.msgtip({
				tipType : "e",
				msg : "最大上行速度不能为空"
			});
			return;
		}
		if (readLimit.length == 0) {
			plugin.msgtip({
				tipType : "e",
				msg : "最大下行速度不能为空"
			});
			return;
		}
		var flag = validate(writeLimit);
		if (!flag) {
			plugin.msgtip({
				tipType : "e",
				msg : "最大上行速度范围【1-12500】,请正确输入"
			});
			return;
		}
		flag = validate(readLimit);
		if (!flag) {
			plugin.msgtip({
				tipType : "e",
				msg : "最大下行速度范围【1-12500】,请正确输入"
			});
			return;
		}
	}
	var lockTime;
	if (lockSel.checked) {
		lockTime = $('#lockTimeSelect option:selected').val();
	} else {
		lockTime = -1;
	}
	var service = new EbeimService();
	var json = {};
	json.writeLimit = writeLimit;
	json.readLimit = readLimit;
	json.lockTime = lockTime;
	json.netcutSel = netCut;
	// json.rebuildsysSel = getSelStatus('rebuildsysSel');
	// json.rebuildserSel = getSelStatus('rebuildserSel');
	// json.saveboxSel = getSelStatus('saveboxSel');
	json.softwaretipSel = getSelStatus('softwaretipSel');
	json.tasktipSel = getSelStatus('tasktipSel');
	json.noticetipSel = getSelStatus('noticetipSel');

	service.setSystemInfo(json, callback);
}

function setLockTime(time) {
	var lockSel = $("#lockSel");
	if (time == -1) {
		lockSel.prop("checked", false);
		$("#lockTimeSelect").attr("disabled", "disabled");
	} else {
		lockSel.prop("checked", true);
		lockSel.siblings("label").addClass("check-true");
//		$("#lockTimeSelect").find("option[value='" + time + "']").attr(
//				"selected", true);
		$("#lockTimeSelect").val(time);
		$("input[name='lockTime']").val(time);
		$("#lockTimeSelect").removeAttr("disabled");
	}
}
function setSel(name, value) {
	var sysSel = $("#" + name);
	if (value == 1) {
		sysSel.prop("checked", true);
		sysSel.siblings("label").addClass("check-true");
	} else {
		sysSel.prop("checked", false);
	}

}
function netcutSel(name, value) {
	var sysSel = $("#" + name);
	if (value == 1) {
		$("input[name='writeLimit']").attr("disabled", "disabled");
		$("input[name='readLimit']").attr("disabled", "disabled");
		sysSel.prop("checked", true);
		sysSel.siblings("label").addClass("check-true");
	} else {
		sysSel.prop("checked", false);
		$("input[name='writeLimit']").removeAttr("disabled");
		$("input[name='readLimit']").removeAttr("disabled");
	}
}
